package com.weather.quartz.job;

import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.weather.api.WeatherAPI;
import com.weather.sendemail.SendEmailUtil;

public class EmailJob extends QuartzJobBean{
	
	// "101280101", "101250801"
	private String [] citys = {"101280601", "101280101"};
	private StringBuilder weather = new StringBuilder("请查收今日天气：");
	
	@Autowired
	SendEmailUtil sendEmailUtil;

	@Autowired
	WeatherAPI weatherAPI;
	
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		
		try {
		
			for(int i=0; i<citys.length; i++) {
				Map<String, Object> todayWeather = weatherAPI.getTodayWeather1(citys[i]);
				
				String description = (String) todayWeather.get("description")+"  /";
				weather.append(description);
			}
			
			String to = (String) context.getMergedJobDataMap().get("to");
			String cc = (String) context.getMergedJobDataMap().get("cc");
			sendEmailUtil.sendSimpleMailMessge(to, cc, weather.toString().substring(0, weather.toString().length()-1), "祝您身体健康！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

}
